/*
 * The RavenX Library
 * Copyright (c) 2008 by Solidus
 * 
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.It is provided "as is" without express 
 * or implied warranty.
 *
 */
#include "rx_debug.h"
#include "rx_spec.h"

#include <stdlib.h>
#include <string.h>

RX_SPACE_BEG


#define RX_TRACE_INFO_BUF 4096


/**************************************rx assert**********************************/

static rx_bool RX_STDCALL __default_assert_handler(const rx_wchar *expr, const rx_wchar *file, int line)
{
		wprintf(RX_WIDE_STR("Assertion failed: %s, file %s, line %d\n"), expr, file, line);
		return rx_false;
}


static rx_assert_handler_t __def_assert_hdl = __default_assert_handler;

rx_assert_handler_t rx_set_assert_handler(rx_assert_handler_t handler)
{
		rx_assert_handler_t		tmp = __def_assert_hdl;
		__def_assert_hdl = handler;
		return tmp;
}

void rx_assert_w(const rx_wchar *expr,const rx_wchar *file, int line)
{
		if(__def_assert_hdl != NULL)
		{
				if(!__def_assert_hdl(expr, file, line)) RX_DEBUGGER_TRIGGER();
		}
}




/*************************************debug_trace*******************************************************************/

static void RX_STDCALL rx_default_trace_print(const rx_wchar *pstr)
{
		wprintf(RX_WIDE_STR("%s"), pstr);
}


static rx_trace_func_t g_trace_print = rx_default_trace_print;



rx_trace_func_t rx_set_trace(rx_trace_func_t trace_f)
{
		rx_trace_func_t tmp = g_trace_print;
		g_trace_print = trace_f;
		return tmp;

}




void rx_trace_fmt(const rx_wchar *fmt, ...)
{
		rx_wchar buf[RX_TRACE_INFO_BUF];
		va_list arg_ptr;
		va_start(arg_ptr, fmt);
		rx_vswprintf(buf, RX_TRACE_INFO_BUF, fmt, arg_ptr);
		va_end(arg_ptr);

		if(g_trace_print != NULL)
		{
				g_trace_print(buf);
		}
}


void rx_trace_fmt_a(const rx_char *fmt, ...)
{
		
		rx_char buf[RX_TRACE_INFO_BUF];
		rx_wchar wbuf[RX_TRACE_INFO_BUF];

		va_list arg_ptr;
		va_start(arg_ptr, fmt);
		rx_vsprintf(buf, RX_TRACE_INFO_BUF, fmt, arg_ptr);
		va_end(arg_ptr);
		
		;

		if(__cstr_to_wstr(buf, wbuf) > 0 && g_trace_print != NULL)
		{
				g_trace_print(wbuf);
		}


}

/*************************************fatal_error*******************************************************************/

static void RX_STDCALL default_fatal_handler(const rx_wchar *pstr)
{
		wprintf(RX_WIDE_STR("%s\n"), pstr);
		abort();
}


rx_fatal_handler_t g_fatal_handler = default_fatal_handler;



rx_fatal_handler_t rx_set_fatal_handler(rx_fatal_handler_t fatal_handler)
{
		rx_fatal_handler_t tmp = g_fatal_handler;
		g_fatal_handler = fatal_handler;
		return tmp;
}


void rx_fatal_fmt(const rx_wchar *fmt, ...)
{
		va_list arg_ptr;
		rx_wchar buf[RX_TRACE_INFO_BUF];
		va_start(arg_ptr, fmt);
		rx_vswprintf(buf, RX_TRACE_INFO_BUF, fmt, arg_ptr);
		va_end(arg_ptr);
		
		if(g_fatal_handler != NULL) g_fatal_handler(buf);


}


void rx_fatal_fmt_a(const rx_char *fmt, ...)
{
		rx_char buf[RX_TRACE_INFO_BUF];
		rx_wchar wbuf[RX_TRACE_INFO_BUF];

		va_list arg_ptr;
		va_start(arg_ptr, fmt);
		rx_vsprintf(buf, RX_TRACE_INFO_BUF, fmt, arg_ptr);
		va_end(arg_ptr);
		
		;

		if(__cstr_to_wstr(buf, wbuf) > 0 && g_fatal_handler != NULL)
		{
				g_fatal_handler(wbuf);
		}
}



























RX_SPACE_END

